package com.yj.rselasticsearch.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yj.common.constant.DataConstants;
import com.yj.common.constant.DataScopeConstants;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.core.domain.entity.*;
import com.yj.common.core.domain.model.LoginUser;
import com.yj.common.utils.FastUtils;
import com.yj.common.utils.SecurityUtils;
import com.yj.common.utils.StringUtils;
import com.yj.rselasticsearch.domain.dto.TagInfoDto;
import com.yj.rselasticsearch.domain.vo.DeptVo;
import com.yj.rselasticsearch.domain.vo.SysDeptFileType;
import com.yj.rselasticsearch.service.FileInfoService;
import com.yj.rselasticsearch.service.FileTypeService;
import com.yj.rselasticsearch.service.TagInfoService;
import com.yj.system.domain.SysRoleDept;
import com.yj.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (tagInfo)表控制层
 *
 * @author xxxxx
 */
@RestController
@Slf4j
@RequestMapping("/tagInfo")
public class TagInfoController {
    /**
     * 服务对象
     */
    @Resource
    private TagInfoService tagInfoService;

    @Resource
    private ISysDeptService sysDeptService;

    @Resource
    private FileInfoService fileInfoService;

    @Resource
    private FileTypeService fileTypeService;


    /**
     * 新增文件类型
     *
     * @param tagInfo
     * @return
     */
    @PutMapping("add")
    public AjaxResult add(@RequestBody TagInfo tagInfo) {
        tagInfo.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return AjaxResult.success(tagInfoService.save(tagInfo));
    }

    /**
     * 文件类型列表
     *
     * @return
     */
    @GetMapping("list")
    public AjaxResult list() {
        return  tagInfoService.getAllList();

    }

    /**
     * 文件类型列表
     *
     * @return
     */
    @PostMapping("pageList")
    public AjaxResult pageList(@RequestBody TagInfoDto tagInfoDto) {
        return tagInfoService.getAllListByPage(tagInfoDto);
    }

    /**
     * 更新标签
     *
     * @param tagInfo
     * @return
     */
    @PutMapping("update")
    public AjaxResult update(@RequestBody TagInfo tagInfo) {
        //如果文档类型已经被引用，则不允许被删除
        List<FileInfo> fileInfoList = fileInfoService.list(new LambdaQueryWrapper<FileInfo>().
                eq(FileInfo::getTagId,tagInfo.getTagId()).eq(FileInfo::getStatus,0));
        if(fileInfoList!=null&&fileInfoList.size()>0){
            return AjaxResult.error(500,"当前分类存在已经使用得文档数据，请先删除文档数据！");
        }
        tagInfo.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return AjaxResult.success(tagInfoService.updateById(tagInfo));
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        //如果文档类型已经被引用，则不允许被删除
        List<FileInfo> fileInfoList = fileInfoService.list(new LambdaQueryWrapper<FileInfo>().
                eq(FileInfo::getTagId,id).eq(FileInfo::getStatus,0));
        if(fileInfoList!=null&&fileInfoList.size()>0){
            return AjaxResult.error(500,"当前分类存在已经使用得文档数据，请先删除文档数据！");
        }
        return tagInfoService.deleteTagInfoById(id);
    }
}
